﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 菜单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "RBAC")]
    public class MenuController : BaseController
    {
        private readonly IMenuService menuService;
        private readonly IMenuActionService menuActionService;
        public MenuController(IWebHostEnvironment environment, IMenuService menuService, IMenuActionService menuActionService) : base(environment)
        {
            this.menuService = menuService;
            this.menuActionService = menuActionService;
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuItem(Menu m)
        {
            string message = string.Empty;
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = menuService.AddMenuItem(m);

            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "200"));
        }

        /// <summary>
        /// 分配权限列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuActionList(long rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = menuService.GetMenuActionList(rid);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRoleMenuAction(CRoleMenuAction m)
        {
            if (m.Rid <= 0)
            {
                return Ok(BaseResult.Result(true, "添加失败！", "400"));
            }
            bool IsOK = menuService.AddRoleMenuAction(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "分配成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "分配失败！", "500"));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMenuItem(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            ApiResult apiResult = new ApiResult();
            bool IsOK = menuService.DelMenuItem(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdMenuItem(Menu m)
        {
            string message = string.Empty;
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = menuService.UpdMenuItem(m);

            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "200"));
        }

        /// <summary>
        /// 根据id获取对象
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuItem(long Id)
        {
            if (Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var list = menuService.GetMenuItem(Id);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败", "500"));
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuList(string? mname = "", int status = 2)
        {
            var list = menuService.GetMenuList(mname, status);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败", "500"));
        }

        /// <summary>
        /// 添加菜单按钮关系
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMenuAction(CMenuAction m)
        {
            bool IsOK = menuActionService.AddMenuAction(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(IsOK, "分配成功！", "200"));
            }
            return Ok(BaseResult.Result(IsOK, "分配失败！", "500"));
        }
    }
}
